#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<unistd.h>
#include<fcntl.h>
#define PUBLIC_FIFO "MYFIFO"
#define PRIVATE1 "P_FO1"
#define PRIVATE1_HEADER "private 1 > "

int main(int argc,char *argv[]){
    int fd;
    int fd1;
    char buf[120];
    char buf1[120]=PRIVATE1_HEADER;
    char num[100]={0};

    //fd=open(PUBLIC_FIFO,O_WRONLY);
    fd1=open(PRIVATE1, O_RDONLY);
    if(read(fd1, buf, 120)!=-1){
        printf("%s\n", buf);
        close(fd1);
    }
    //printf("Read success!\n");

    while(1){
        //printf("Open PUBLIC_FIFO\n");
        if((fd=open(PUBLIC_FIFO,O_WRONLY))==-1){
            printf("open failed\n" );
            exit(1);
        }
        printf("Please input : ");
        fgets(num, 100, stdin);
        if(strncmp(num, "end", 3)==0){
            break;
        }
        strcpy(buf1, PRIVATE1_HEADER);
        strcat(buf1, num);
        if(write(fd, buf1, 220) > 0){
            printf("Write Data Success!\n");
            close(fd);
        }

        fd1=open(PRIVATE1, O_RDONLY);
        if(read(fd1, buf, 120)!=-1){
            printf("%s\n\n", buf);
            close(fd1);
        }
    }
    close(fd);
    return 0;
}
